package cx3k.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import cx3k.scheduler.Scheduler;
import cx3k.scheduler.objects.Course;

public class SwapPane extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SwapClassDisplay swap1;
	private SwapClassDisplay swap2;
	private JButton jbSwap;
	private int numCourses;
	private ClassInfo classInfo;
	private Box hbox;
	private RemoveListener RL;
	private Scheduler scheduler;
	private int scheduleID;
	
	public SwapPane(ClassInfo classInfo, Scheduler s) {
		this.setBackground(MainWindow.MAINCOLOR);
		this.RL = new RemoveListener();
		this.setSwap1(new SwapClassDisplay());
		this.setSwap2(new SwapClassDisplay());
		this.setJbSwap(new JButton("SWAP"));
		this.setNumCourses(0);
		this.classInfo = classInfo;
		this.scheduler = s;
		
		setHbox(Box.createHorizontalBox());
		getHbox().add(new JLabel("Swap Classes: "));
		getHbox().add(getSwap1());
		getHbox().add(getSwap2());
		getHbox().add(getJbSwap());
		this.add(getHbox());
	}

	public void addCourse(Course c) {
		this.addCourse(new ScheduledCourseGroup(c));
	}
	
	public void addCourse(ScheduledCourseGroup c) {
		if (getNumCourses() == 0) {
			this.getHbox().remove(getSwap1());
			setSwap1(new SwapClassDisplay(c, classInfo));
			getSwap1().jbRemove.addActionListener(RL);
			this.getHbox().add(getSwap1(), 1);
			this.setNumCourses(this.getNumCourses() + 1);
			this.updateUI();
		}
		else if (getNumCourses() == 1) {
			this.getHbox().remove(getSwap2());
			setSwap2(new SwapClassDisplay(c, classInfo));
			getSwap2().jbRemove.addActionListener(RL);
			this.getHbox().add(getSwap2(), 2);
			this.setNumCourses(this.getNumCourses() + 1);
			this.updateUI();
		}
	}
	
	public void setScheduleID(int scheduleID) {
		this.scheduleID = scheduleID;
	}

	public int getScheduleID() {
		return scheduleID;
	}
	
	public int getNumCourses() {
		return numCourses;
	}
	
	public SwapClassDisplay getSwap1() {
		return swap1;
	}
	
	public SwapClassDisplay getSwap2() {
		return swap2;
	}

	public void setSwap1(SwapClassDisplay swap1) {
		this.swap1 = swap1;
	}

	public void setSwap2(SwapClassDisplay swap2) {
		this.swap2 = swap2;
	}

	public void setNumCourses(int numCourses) {
		this.numCourses = numCourses;
	}

	public void setHbox(Box hbox) {
		this.hbox = hbox;
	}

	public Box getHbox() {
		return hbox;
	}

	public void setJbSwap(JButton jbSwap) {
		this.jbSwap = jbSwap;
	}

	public JButton getJbSwap() {
		return jbSwap;
	}

	private class SwapListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			if (getNumCourses() == 2) {
				scheduler.swapclass(scheduleID, getSwap1().getCourseGroup(), getSwap2().getCourseGroup());
				getHbox().remove(getSwap1());
				getHbox().remove(getSwap2());
				setSwap1(new SwapClassDisplay());
				setSwap2(new SwapClassDisplay());
				setNumCourses(getNumCourses() - 2);
				getHbox().add(getSwap1(), 1);
				getHbox().add(getSwap2(), 2);
				updateUI();
			}
		}
		
	}
	
	private class RemoveListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			if (ae.getSource() == getSwap1().jbRemove) {
				getHbox().remove(getSwap1());
				if (getSwap2().PLACEHOLDER == false) {
					getHbox().remove(getSwap2());
					setSwap1(getSwap2());
					setSwap2(new SwapClassDisplay());
					getHbox().add(getSwap1(), 1);
					getHbox().add(getSwap2(), 2);
				}
				else {
					setSwap1(new SwapClassDisplay());
					getHbox().add(getSwap1(), 1);
				}
				setNumCourses(getNumCourses() - 1);
				updateUI();
			}
			else if (ae.getSource() == getSwap2().jbRemove) {
				getHbox().remove(getSwap2());
				setSwap2(new SwapClassDisplay());
				getHbox().add(getSwap2(), 2);
				setNumCourses(getNumCourses() - 1);
				updateUI();
			}
		}
		
	}
}